package cn.xsw.qrcode_login.controller;

import cn.xsw.qrcode_login.entity.UserToken;
import cn.xsw.qrcode_login.form.input.ConfirmInput;
import cn.xsw.qrcode_login.form.input.GenQrInput;
import cn.xsw.qrcode_login.form.input.ScanInput;
import cn.xsw.qrcode_login.mapper.QrTokenMapper;
import cn.xsw.qrcode_login.util.QRCodeUtil;
import com.google.zxing.WriterException;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.BeanUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@CrossOrigin
@RestController
public class QrScanController {

    @Resource
    QrTokenMapper qrTokenMapper;

    // 生成二维码
    @PostMapping("/get_qr_code")
    public Map<String, Object> genQr(@RequestBody GenQrInput in) {
        String tempPngName = UUID.randomUUID().toString().substring(0, 8);
        // 创建二维码记录
        UserToken userToken = qrTokenMapper.queryQrToken(in.getDeviceInf(), in.getDeviceType());
        if (userToken == null) {
            userToken = new UserToken();
            BeanUtils.copyProperties(in, userToken);
            Integer count = qrTokenMapper.createQrToken(userToken);
        }
        Map<String, Object> ret = new HashMap<>();
        // 生成二维码
        try {
            QRCodeUtil.generateQRCodeImage(userToken.getId() + "", 200, 200, "D://LenovoQMDownload/" + tempPngName+ ".png");
        } catch (Exception e) {
            e.printStackTrace();
        }
        ret.put("url", "D://LenovoQMDownload/" + tempPngName+ ".png");
        ret.put("qrId", userToken.getId());
        return ret;
    }

    // 获取二维码状态
    @GetMapping("/get_qr_code_status")
    public Object getQrTokenStatus(@RequestParam("qrId") Integer qrId) {
        Map<String, Object> ret = new HashMap<>();
        UserToken userToken = qrTokenMapper.queryQrTokenById(qrId);
        if (userToken == null) {
            ret.put("msg", "二维码已经失效");
            ret.put("success", false);
            return ret;
        }
        if (ObjectUtils.nullSafeEquals(2, userToken.getQcStatus())) {
            ret.put("token", userToken.getToken());
        }
        ret.put("status", userToken.getQcStatus());
        ret.put("success", true);
        return ret;
    }

    // 我已扫码
    @PostMapping("/i_scan")
    public Object iScan(@RequestBody ScanInput in) {
        Map<String, Object> ret = new HashMap<>();
        String tempToken = UUID.randomUUID().toString().replace("-", "");
        int count = qrTokenMapper.updateTempTokenQrTokenById(in.getQrId(), in.getUserId(), tempToken);
        if (count < 1) {
            ret.put("msg", "扫码失败，请重试");
            ret.put("success", false);
            return ret;
        }
        UserToken userToken = qrTokenMapper.queryQrTokenById(in.getQrId());
        ret.put("deviceType", userToken.getDeviceType());
        ret.put("status", 1);
        ret.put("token", tempToken);
        ret.put("success", true);
        return ret;
    }

    // 扫码确认
    @PostMapping("/i_confirm")
    public Object iConfirm(@RequestBody ConfirmInput in) {
        Map<String, Object> ret = new HashMap<>();
        UserToken userToken = qrTokenMapper.queryQrTokenByTempToken(in.getQrId(), in.getTempToken());
        if (userToken == null) {
            ret.put("msg", "扫码失败，请重新二维码重试");
            ret.put("success", false);
            return ret;
        }
        String newToken = UUID.randomUUID().toString().replace("-", "");
        int count = qrTokenMapper.updateTokenQrTokenById(in.getQrId(), newToken);
        if (count < 1) {
            ret.put("msg", "扫码失败，请重新二维码重试");
            ret.put("success", false);
            return ret;
        }
        ret.put("status", 2);
        ret.put("success", true);
        return ret;
    }

    // 扫码确认
    @GetMapping("/logout")
    public Object logout(@RequestParam("userId") Integer userId) {
        Map<String, Object> ret = new HashMap<>();
        qrTokenMapper.removeUserTokenByUserId(userId);
        return ret;
    }
}
